package com.syzg.core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil
{
  public static final String DATE_PATTERN = "yyyy-MM-dd";
  public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
  public static final String TIME_PATTERN = "HH:mm:ss";

  public static String format(long millis, String pattern)
  {
    return format(new Date(millis), pattern);
  }

  public static String format(Date date, String pattern) {
    DateFormat formatter = new SimpleDateFormat(pattern);
    return formatter.format(date);
  }

  public static String formatDate(Date date) {
    return format(date, "yyyy-MM-dd");
  }

  public static String formatTime(Date date) {
    return format(date, "HH:mm:ss");
  }

  public static String formatDateTime(Date date) {
    return format(date, "yyyy-MM-dd HH:mm:ss");
  }

  public static String formatCurrent(String pattern) {
    return format(new Date(), pattern);
  }

  public static String formatCurrentDate() {
    return format(new Date(), "yyyy-MM-dd");
  }

  public static String formatCurrentTime() {
    return format(new Date(), "HH:mm:ss");
  }

  public static String formatCurrentDateTime() {
    return format(new Date(), "yyyy-MM-dd HH:mm:ss");
  }

  public static Date getCurrentDate() {
    Calendar cal = Calendar.getInstance();
    cal.set(11, 0);
    cal.set(12, 0);
    cal.set(13, 0);
    cal.set(14, 0);
    return cal.getTime();
  }

  public static Date getTheDate(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(11, 0);
    cal.set(12, 0);
    cal.set(13, 0);
    cal.set(14, 0);
    return cal.getTime();
  }

  public static int compareDate(Date start, Date end) {
    if ((start == null) && (end == null)) {
      return 0;
    }
    if (end == null) {
      return 1;
    }
    if (start == null) {
      start = new Date();
    }
    start = getTheDate(start);
    end = getTheDate(end);
    return start.compareTo(end);
  }

  public static Date parse(String dateString, String pattern) {
    DateFormat formatter = new SimpleDateFormat(pattern);
    try
    {
      return formatter.parse(dateString);
    } catch (ParseException e) {
      throw new RuntimeException(e);
    }
  }

  public static Date addYears(Date date, int amount) {
    return add(date, 1, amount);
  }

  public static Date addMonths(Date date, int amount) {
    return add(date, 2, amount);
  }

  public static Date addWeeks(Date date, int amount) {
    return add(date, 3, amount);
  }

  public static Date addDays(Date date, int amount) {
    return add(date, 5, amount);
  }

  public static Date addHours(Date date, int amount) {
    return add(date, 11, amount);
  }

  public static Date addMinutes(Date date, int amount) {
    return add(date, 12, amount);
  }

  public static Date addSeconds(Date date, int amount) {
    return add(date, 13, amount);
  }

  public static Date addMilliseconds(Date date, int amount) {
    return add(date, 14, amount);
  }

  private static Date add(Date date, int calendarField, int amount) {
    if (date == null) {
      throw new IllegalArgumentException("日期对象不允许为null!");
    }
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(calendarField, amount);
    return c.getTime();
  }

  public static Date getDateAfterNatureDays(Date date, int days, int hours)
  {
    if (date == null) {
      throw new IllegalArgumentException("日期对象不允许为null!");
    }
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(c.get(5), days);
    c.add(c.get(10), hours);
    return c.getTime();
  }

  public static Date getDateAfterWorkDays(Date date, int days, int hours)
  {
    if (date == null) {
      throw new IllegalArgumentException("日期对象不允许为null!");
    }
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(c.get(5), days);
    c.add(c.get(10), hours);
    return c.getTime();
  }
}